<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/common.jsp"%>
<%
	Message msg = new Message();
	try {

		String cardNo = input.getDesc("cardNo",""); // 身份证号
		
		// 根据身份证号我的预约
		Connection connection = null;
		PreparedStatement  statement = null;
		try {
			connection = getConn();
			String sql="		select RTRIM(LTRIM(k.jssjh)) sjh,t4.name ysname, m.patid,m.name ksname, m.xmdj,convert(date,m.yyrq, 20) yyrq, m.jlzt,m.hzxm,m.ysdm,m.yydm, case m.yydm when '01' then '安镇院区' when '02' then '广瑞路院区' end yqmc, m.ksdm,m.xh,m.sjd,m.zjxh as pbmxxh  from ( " 
											+ " select t1.*,t2.xmdj,t3.name,t3.yydm from GH_YY_PBZBMX t0, GH_SH_GHYYK t1, YY_SFXXMK t2, YY_KSBMK t3 "
											+ " where  t0.pbmxid=t1.zjxh and t0.zlfdm = t2.id and t1.patid in ( "
											+ " select DISTINCT patid from SF_BRXXK where sfzh=? "
											+ " )  and t1.ksdm=t3.id "
											+ " ) m LEFT JOIN GH_GHZDK k "
											+ "  on m.patid=k.patid and m.xh = k.yyxh and k.jlzt in(0,1) LEFT JOIN YY_ZGBMK t4 on t4.id=m.ysdm ORDER BY jlzt asc,yyrq desc,sjd asc "; // 加上这个条件去除红冲记录从而去掉重复记录k.jlzt in(0,1)
			statement = connection.prepareStatement(sql); 
			statement.setString(1, cardNo);
			ResultSet rs = statement.executeQuery();
			List list = new ArrayList();
			Map m= null;
			while(rs.next()){
				 m = new HashMap();
				 m.put("xh",rs.getString("xh"));
				 m.put("sjh",rs.getString("sjh"));
				 m.put("xmdj",rs.getString("xmdj"));
				 m.put("jlzt",rs.getString("jlzt"));
				 m.put("hzxm",rs.getString("hzxm"));
				 m.put("yyrq",rs.getString("yyrq"));
				 m.put("sjd",rs.getString("sjd"));
				 m.put("ksdm",rs.getString("ksdm"));
				 m.put("ksname",rs.getString("ksname"));
				 m.put("ysdm",rs.getString("ysdm"));
				 
				 m.put("yydm",rs.getString("yydm")); // 院区代码
				 m.put("yqmc",rs.getString("yqmc")); // 院区名称
				 m.put("ysname",rs.getString("ysname"));		
				 m.put("pbmxxh",rs.getString("pbmxxh"));	
				 m.put("patid",rs.getString("patid"));	
				 list.add(m);	 				 				 				 				 
      }
      msg.put("data",list);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
					if (statement != null)
						statement.close(); 
					if (connection != null)
						connection.close(); 
				} catch (Exception e) {
					statement = null;
					connection = null;
					e.printStackTrace();
				}
		}
		
		msg.put("errcode",0);
    }catch (Exception e){
	   	msg.put("errcode",FAILURE);
    }
		out.println(msg);
%>